package com.wanxing.main;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wanxing.main.ControlFragment.myAsyncTask2;
import com.util.HttpUtils;
import com.util.Info;
import com.zhny.wxginger.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Dialog_water_Activity extends Activity {

	String maxhumi;
	String minhumi;
	String ntime;
	String DUT;

	PickerView bfpoint1;
	PickerView bfpoint2;
	PickerView bfpoint3;

	private String DTU = null;
	private Handler handler;
	private ArrayAdapter<String> lab_adapter;
	boolean flag = false;
	Boolean s_switch;

	Button b2;// ����

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_water);

		Intent intent = getIntent();
		DTU = intent.getStringExtra("dtu");
		System.out.println(DTU + "-----get DTU");

		setWindsize();
		fv();
		setlinser();

		new myAsyncTask().execute(Info.path8 + DTU);// ��ѯ��ʷ

	}

	private void setWindsize() {
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay(); // Ϊ��ȡ��Ļ����

		LayoutParams p = getWindow().getAttributes(); // ��ȡ�Ի���ǰ�Ĳ���ֵ
		p.height = (int) (d.getHeight() * 0.6); // �߶�����Ϊ��Ļ��1.0
		p.width = (int) (d.getWidth() * 0.6); // �������Ϊ��Ļ��0.8
		p.alpha = 1.0f; // ���ñ���͸����
		p.dimAmount = 0.0f; // ���úڰ���

		getWindow().setAttributes(p); // ������Ч
		getWindow().setGravity(Gravity.CENTER); // ���ÿ��Ҷ���
	}

	private void fv() {
		bfpoint1 = (PickerView) findViewById(R.id.w_bfpoint1);
		bfpoint2 = (PickerView) findViewById(R.id.w_bfpoint2);
		bfpoint3 = (PickerView) findViewById(R.id.w_bfpoint3);

		b2 = (Button) findViewById(R.id.w_Button2);

	}

	private void setlinser() {
		// ������Ϊ������ֱ���������Դ
		List<String> inte1 = new ArrayList<String>();// ���bf1�������ֵ�����
		for (int i = 0; i < 100; i++)// 0~99
		{
			inte1.add(i < 10 ? "0" + i : "" + i);
		}

		List<String> inte2 = new ArrayList<String>();// ���bf2�������ֵ�����
		for (int i = 0; i < 100; i++)// 0~99
		{
			inte2.add(i < 10 ? "0" + i : "" + i);
		}
		List<String> inte3 = new ArrayList<String>();// ���bf3�������ֵ�����
		for (int i = 0; i < 100; i++)// 0~99
		{
			inte3.add(i < 10 ? "0" + i : "" + i);
		}

		bfpoint1.setData(inte1);
		bfpoint1.setOnSelectListener(new PickerView.onSelectListener() {
			public void onSelect(String text) {
				maxhumi = text;
				// Toast.makeText(getActivity(), text, 1).show();
				b2.setClickable(true);
				b2.setBackgroundResource(R.drawable.btn_selector);
				flag = true;

				// ��߿���дѡ���¼�
			}
		});
		// bfpoint1.setSelected(10);//ͨ��������� ���Ի�����ݿ��е���ֵ������PICKERVIEW��ʾ��Ĭ��ֵ
		bfpoint2.setData(inte2);
		bfpoint2.setOnSelectListener(new PickerView.onSelectListener() {

			@Override
			public void onSelect(String text) {
				minhumi = text;
				b2.setClickable(true);
				b2.setBackgroundResource(R.drawable.btn_selector);
				flag = true;

			}
		});
		// bfpoint2.setSelected(30);

		bfpoint3.setData(inte3);
		bfpoint3.setOnSelectListener(new PickerView.onSelectListener() {

			@Override
			public void onSelect(String text) {

				ntime = text;
				b2.setClickable(true);
				b2.setBackgroundResource(R.drawable.btn_selector);
				flag = true;
			}
		});

		b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog1();

			}
		});
		b2.setClickable(false);
	}

	private void showDialog1() {
		LayoutInflater inflater = LayoutInflater
				.from(Dialog_water_Activity.this);
		final View view = inflater.inflate(R.layout.dialog_item, null);
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(Dialog_water_Activity.this);
		builder.setTitle("���������������֤");
		builder.setView(view);
		builder.setPositiveButton("�ύ", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				EditText id = (EditText) view.findViewById(R.id.ed_id);
				String sid = id.getText().toString();
				if (sid.equals("112233")) {
					// if(DTU.equals("001")){
					// String
					// command="BB01dezhoulx0001#13"+maxhumi+":"+minhumi+":"+ntime+:$";
					// //command="BB01dezhoulx0001#13"+maxhumi+":"+minhumi+":"+ntime+":"+nyear+":"+nmonth+":"+nday+":"+nhour+":"+nmintue+":$";
					// new Thread(new Sendcommand(handler,
					// command)).start();
					// }else if(DTU.equals("002")){
					// String
					// command="BB01dezhoulx0002#13"+maxhumi+":"+minhumi+":"+ntime+":$";
					// //command="BB01weifang0002#13"+maxhumi+":"+minhumi+":"+ntime+":"+nyear+":"+nmonth+":"+nday+":"+nhour+":"+nmintue+":$";
					// new Thread(new Sendcommand(handler,
					// command)).start();
					// }else if(DTU.equals("006")){
					// String
					// command="BB01dezhoulx0002#13"+maxhumi+":"+minhumi+":"+ntime+":$";
					// //command="BB01weifang0002#13"+maxhumi+":"+minhumi+":"+ntime+":"+nyear+":"+nmonth+":"+nday+":"+nhour+":"+nmintue+":$";
					// new Thread(new Sendcommand(handler,
					// command)).start();
					// }
					String command = "BB00dezhoulx" + DTU + "#13"+maxhumi+":"+minhumi+":"+ntime+":$";

				} else {
					Toast.makeText(Dialog_water_Activity.this, "��֤ʧ��", 1)
							.show();
				}
				Toast.makeText(Dialog_water_Activity.this, "�ù�����ʱ�޷�ʹ��", 1)
						.show();
			}
		});
		builder.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();

	}

	class myAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String result = HttpUtils.sendPostMethod(params[0], "utf-8");
			System.out.println(result);
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			System.out.println(result);
			List<String> list = new ArrayList<String>();
			if (result == null) {
				list.add("" + 0);
				list.add("" + 0);
				list.add("" + 0);
				Toast.makeText(Dialog_water_Activity.this, "��ѯʧ��", 1).show();
			} else if (result.equals("fail")) {
				list.add("" + 0);
				list.add("" + 0);
				list.add("" + 0);
				Toast.makeText(Dialog_water_Activity.this, "��ѯʧ��", 1).show();
			} else {
				try {
					JSONArray array = new JSONArray(result);
					JSONObject jsonObject = new JSONObject();
					for (int i = 0; i < array.length(); i++) {
						jsonObject = array.getJSONObject(i);

						int a = (int) Double.parseDouble(jsonObject
								.getString("soil_up"));
						int b = (int) Double.parseDouble(jsonObject
								.getString("soil_down"));
						int c = (int) Double.parseDouble(jsonObject
								.getString("soil_time"));

						list.add("" + a);
						list.add("" + b);
						list.add("" + c);

					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(list.get(0));
				System.out.println(list.get(1));
				System.out.println(list.get(2));
				bfpoint1.setSelected(list.get(0));
				bfpoint2.setSelected(list.get(1));
				bfpoint3.setSelected(list.get(2));
				maxhumi = list.get(0);
				minhumi = list.get(1);
			}

		}

	}

}
